<?php

namespace App\Http\Controllers\Admin;

use App\Models\GoodsClass;
use App\Models\GoodsType;
use App\Requests\Admin\GoodsType\GoodsTypeIndexRequest;
use App\Requests\Admin\GoodsType\GoodsTypeSaveRequest;
use App\Requests\Admin\GoodsType\GoodsTypeSwitchStatusRequest;
use App\Requests\BaseRequest;
use App\Traits\CurdTrait;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class GoodsTypeController extends BaseController
{
    use CurdTrait;

    /**
     * @var GoodsType
     */
    protected $curdModel;

    public function _init()
    {
        parent::_init();
        $this->setCurdModel(new GoodsType());
    }

    /**
     * 列表
     *
     * @param GoodsTypeIndexRequest $request
     *
     * @author Chris Yu <chrisyu@crabapple.top> 2023/11/16
     */
    public function list(GoodsTypeIndexRequest $request)
    {
        $paramId = $request->input('id');
        $paramName = $request->input('name');
        $paramBeginTime = $request->input('begin_time');
        $paramEndTime = $request->input('end_time');
        $paramStatus = $request->input('status');

        $paginate = $this->curdModel
            ->with(['brandInfo' => function($q) {
                $q->select(['id', 'name']);
            }, 'goodsClassInfo' => function($q) {
                $q->select(['id', 'name']);
            }])
            ->when($paramId, function ($q) use ($paramId) {
                $q->where('id', $paramId);
            })
            ->when($paramName, function ($q) use ($paramName) {
                $q->where('name', 'like', '%' . $paramName . '%');
            })
            ->when($paramBeginTime, function ($q) use ($paramBeginTime) {
                $q->where('create_time', '>=', $paramBeginTime);
            })
            ->when($paramEndTime, function ($q) use ($paramEndTime) {
                $q->where('create_time', '<=', $paramEndTime);
            })
            ->when($paramStatus, function ($q) use ($paramStatus) {
                $q->where('status', $paramStatus);
            })
            ->orderBy('id', 'desc')
            ->paginate($this->pageSize);

        return $this->responseSuccess('', [
            'list' => $paginate->items(),
            'total' => $paginate->total(),
        ]);
    }

    /**
     * 详情
     * @param \Illuminate\Http\Request $request
     *
     * @author Chris Yu <chrisyu@crabapple.top> 2023/11/9
     */
    public function info(Request $request)
    {
        $id = $request->input('id');
        $model = $this->curdModel->find($id);
        if (empty($model)) {
            return $this->responseFail('数据不存在');
        }

        return $this->responseSuccess('', $model);
    }

    /**
     * 保存
     * @param \Illuminate\Foundation\Http\FormRequest $request
     *
     * @return \Illuminate\Http\JsonResponse
     * @author Chris Yu <chrisyu@crabapple.top> 2023/11/9
     */
    public function save(GoodsTypeSaveRequest $request)
    {
        $id = $request->input('id');
        if ($id) {
            return $this->edit($request);
        } else {
            return $this->add($request);
        }
    }

    /**
     * 添加
     * @param \Illuminate\Foundation\Http\FormRequest $request
     *
     * @return \Illuminate\Http\JsonResponse
     * @author Chris Yu <chrisyu@crabapple.top> 2024/12/18
     */
    protected function add(GoodsTypeSaveRequest $request)
    {
        $saveData = $request->validated();
        $name = $saveData['name'];

        if ($this->curdModel->where('name', $name)->exists()) {
            return $this->responseFail('名称已存在');
        }

        DB::beginTransaction();

        $row = $this->curdModel->create($saveData);
        if (!$row) {
            return $this->responseFail('添加失败');
        }

        DB::commit();

        return $this->responseSuccess('添加成功', ['id' => $row->getKey()]);
    }

    /**
     * 编辑
     * @param \Illuminate\Foundation\Http\FormRequest $request
     *
     * @return \Illuminate\Http\JsonResponse
     * @author Chris Yu <chrisyu@crabapple.top> 2023/11/9
     */
    protected function edit(GoodsTypeSaveRequest $request)
    {
        $saveData = $request->validated();
        $id = $saveData['id'];

        $row = $this->curdModel->find($id);
        if (empty($row)) {
            return $this->responseFail('数据不存在');
        }

        $name = $saveData['name'];
        if ($this->curdModel->where('name', $name)->where('id', '!=', $id)->exists()) {
            return $this->responseFail('名称已存在');
        }

        unset($saveData['id']);

        DB::beginTransaction();

        $res = $row->fill($saveData)->save();
        if (!$res) {
            return $this->responseFail('编辑失败');
        }

        DB::commit();

        return $this->responseSuccess('编辑成功', ['id' => $row->getKey()]);
    }

    /**
     * 删除
     * @param \Illuminate\Http\Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     * @author Chris Yu <chrisyu@crabapple.top> 2023/11/9
     */
    public function delete(Request $request)
    {
        $id = $request->input('id');

        $row = $this->curdModel->find($id);
        if (empty($row)) {
            return $this->responseFail('数据不存在');
        }

        DB::beginTransaction();

        $res = $row->delete();
        if (!$res) {
            return $this->responseFail('删除失败');
        }

        DB::commit();

        return $this->responseSuccess('删除成功');
    }

    /**
     * 预览
     * @param BaseRequest $request
     *
     * @author Chris Yu <chrisyu@crabapple.top> 2023/11/9
     */
    public function switchStatus(GoodsTypeSwitchStatusRequest $request)
    {
        $id = $request->input('id');
        $status = $request->input('status');
        $row = $this->curdModel->find($id);
        if (empty($row)) {
            return $this->responseFail('数据不存在');
        }

        $row->update([
            'status' => $status,
        ]);

        return $this->responseSuccess();
    }

}
